/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.controller;

import com.vng.zing.survey.thrift.client.SurveyClient;
import com.vng.zing.survey.thrift.model.ShortInfoSurveyItem;
import com.vng.zing.survey.thrift.model.UserType;
import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.vng.zing.survey.model.CommonModel;
import com.vng.zing.survey.model.RenderModel;
import com.vng.zing.survey.thrift.model.ListShortInfoSurvey;
import java.util.Date;
import java.util.Map;

/**
 *
 * @author namhh
 */
public class HomeController extends BaseController {

    private static UserType userType = UserType.USER;

    @Override
    public String render(HttpServletRequest req, HttpServletResponse resp) throws IOException, Exception {

        String content = "";
        TemplateDataDictionary dic = TemplateDictionary.create();

        dic.setVariable("HEADER", CommonModel.getHeader(TemplateDictionary.create(), "Trang khảo sát"));
        dic.setVariable("FOOTER", CommonModel.getFooter(TemplateDictionary.create()));
        Map mapUser = checkMember(req, userType);
        if (mapUser.containsValue(ErrorType.FAIL) || mapUser.containsValue(ErrorType.ERROR_SYSTEM)) {
            return CommonModel.getError(dic, (String) mapUser.get("msg"));
        }
        int idfan = (int) mapUser.get("msg");
        int iduser =(int) mapUser.get("idUser");
        boolean isAdmin = (boolean) mapUser.get("admin");
        SurveyClient surveyClient = SurveyClient.getInstance();
        ListShortInfoSurvey listShortInfoSurvey;
        if (idfan == iduser) {
            listShortInfoSurvey = surveyClient.getgetListShortInfoSurveyAll(idfan, CommonModel.OFFSET, UserType.ADMIN);
             TemplateDataDictionary newIcon = TemplateDictionary.create();
            dic.setVariable("ADMIN", CommonModel.getNewIconSurvey(newIcon));
        } else {
            listShortInfoSurvey = surveyClient.getgetListShortInfoSurveyAll(idfan, CommonModel.OFFSET, UserType.USER);
        }
        List<ShortInfoSurveyItem> newestSurvey=listShortInfoSurvey.getNewlist();
        
        int count = listShortInfoSurvey.getCountnew();
        int today = (int) (new Date().getTime() / 1000);

        TemplateDataDictionary dicNewTable = TemplateDictionary.create();
        if (newestSurvey.isEmpty()) {
            dicNewTable.setVariable("CONTENT", "Chưa có khảo sát nào!");
            content = CommonModel.getEmpty(dicNewTable);
        } else {
            for (ShortInfoSurveyItem t : newestSurvey) {
                TemplateDataDictionary slideItems = dicNewTable.addSection("SLICE_SHOW_ITEMS");
                slideItems.setVariable("RESPONSE_LINK", "response?id=" + t.getIdSurvey());
                slideItems.setVariable("TITLE", t.getTitle());
                slideItems.setVariable("VIEW", String.format("%s", t.getView()));
                slideItems.setVariable("RESPONSE", String.format("%s", t.getResponse()));
                if (isAdmin || t.isOption()) {
                    slideItems.setVariable("LINK", "stat?id=" + t.getIdSurvey());
                } else {
                    slideItems.setVariable("LINK", "#");
                    slideItems.setVariable("DISABLE", "button-zm-dis");
                }
                if (today > t.getEndDay()) {
                    slideItems.setVariable("STATUS", "expired");
                } else if (today < t.getBeginDay()) {
                    slideItems.setVariable("STATUS", "no-active");
                } else {
                    slideItems.setVariable("STATUS", "active");
                }

            }

            content = CommonModel.getNewTable(dicNewTable);
            int page = (int) Math.floor(count / CommonModel.OFFSET);
            System.out.println("count" + page);

            if (page > 1) {
                TemplateDataDictionary dicPage = TemplateDictionary.create();

                /// bo +1
                dicPage.setVariable("COUNT", String.format("%s", page));
                dicPage.setVariable("OFFSET", String.format("%s", CommonModel.OFFSET));
                dicPage.setVariable("NAME", "paging_new");
                String paging = CommonModel.getPaging(dicPage);
                dic.setVariable("PAGE", paging);
            }
        }

        dic.setVariable("NEW_TABLE", content);
        // host
        TemplateDataDictionary dicHostTable = TemplateDictionary.create();
        newestSurvey = listShortInfoSurvey.getHostlist();
        if (newestSurvey.isEmpty()) {
            dicHostTable.setVariable("CONTENT", "Chưa có khảo sát nào!");
            content = CommonModel.getEmpty(dicHostTable);
        } else {
            for (ShortInfoSurveyItem t : newestSurvey) {
                TemplateDataDictionary slideItems = dicNewTable.addSection("SLICE_SHOW_ITEMS");
                slideItems.setVariable("RESPONSE_LINK", "response?id=" + t.getIdSurvey());
                slideItems.setVariable("TITLE", t.getTitle());
                slideItems.setVariable("VIEW", String.format("%s", t.getView()));
                slideItems.setVariable("RESPONSE", String.format("%s", t.getResponse()));
                if (isAdmin || t.isOption()) {
                    slideItems.setVariable("LINK", "stat?id=" + t.getIdSurvey());
                } else {
                    slideItems.setVariable("LINK", "#");
                    slideItems.setVariable("DISABLE", "button-zm-dis");
                }
                if (today > t.getEndDay()) {
                    slideItems.setVariable("STATUS", "expired");
                } else if (today < t.getBeginDay()) {
                    slideItems.setVariable("STATUS", "no-active");
                } else {
                    slideItems.setVariable("STATUS", "active");
                }
            }

            content = CommonModel.getHotTable(dicNewTable);
            int page = (int) Math.floor(listShortInfoSurvey.getCounthost() / CommonModel.OFFSET);
            if (page > 1) {
                TemplateDataDictionary dicPage = TemplateDictionary.create();
                dicPage.setVariable("COUNT", String.format("%s", page));
                dicPage.setVariable("OFFSET", String.format("%s", CommonModel.OFFSET));
                dicPage.setVariable("NAME", "paging_hot");
                String paging = CommonModel.getPaging(dicPage);
                dic.setVariable("PAGEHOST", paging);
            }
        }
        dic.setVariable("HOT_TABLE", content);

        dic.setVariable("LINKNEW", "new");
        Template template = RenderModel.getCTemplate("home");
        content = template.renderToString(dic);
        //setCache(new String[]{String.format("%s", idUser)}, 10, content);
        return content;
    }
}
